Ext.onReady(function(){
	desktop.init();
});
var desktop ={
	init: function(){
		Ext.create('Ext.container.Viewport', {
		    layout: 'border',
		    items: [{
		        region: 'north',
		        bodyStyle: 'background-image:url('+CONTEXT.contextPath+'/web/theme/system/images/headbg.gif);',
		        height: 50,
		        //html: '<h1 class="x-panel-header">亮堂堂汽车维修有限公司</h1>',
		        contentEl: 'head',
		        border: false
		        //margins: '0 0 5 0'
		    }, {
		        region: 'west',
		        //collapsible: true,
		        split: true,
		        //title: 'Navigation',
		        width: 200,
		        layout: 'fit',
		        items:[desktop.createMenuPanel()]
		        // 这里通常可以使用 TreePanel 或者 AccordionLayout布局的导航菜单
		    }/*, {
		        region: 'south',
		        //title: 'South Panel',
		        //collapsible: true,
		        border: false,
		        html: '<div style="text-align:center;height:100%;line-height:30px;">统一开发框架</div>',
		        //split: true,
		        height: 30,
		        minHeight: 30
		    }*/,{
		    	id: 'mainPanel',
		        region: 'center',
		        xtype: 'tabpanel', // TabPanel本身没有title属性
		        activeTab: 0,      // 配置默认显示的激活面板
		        enableTabScroll: true,
				plugins:Ext.create('Ext.ux.TabCloseMenu', {
					closeTabText : '关闭当前',
	                closeOthersTabsText : '关闭其他',
	                closeAllTabsText : '关闭所有',
	                refreshPageText: '刷新',
	                border: 0,
	                frame: false
	                /*extraItemsHead : [{
	                	text: '快捷菜单',
	                    handler : function(menu,item){
	                    	alert(0);
	                    }
	                }],
	                extraItemsTail : ['-', {
	                    text : '可关闭',
	                    checked : true,
	                    hideOnClick : true,
	                    handler : function(item) {
	                        //currentItem.tab.setClosable(item.checked);
	                    	alert(9);
	                    }
	                }],
	                listeners : {
	                	aftermenu : function() {
	                		//alert(8);
	                	},
	                	beforemenu : function(menu, item) {
	                		//alert(7);
	                		//return false;
	                	}
	                }*/
				}),
		        items: {
		            title: '首页',
		            iconCls: "house",
		            frameId: "home",
		            html: '<iframe id="frame_home" name="frame_home" src="" style="width:100%;height:100%;" frameborder="0" scrolling="auth"></iframe>'
		        }
		    }]
		});
	},
	/**
	 * 创建系统菜单
	 * @returns
	 */
	createMenuPanel: function(){
		var menuPanel = Ext.create('Ext.panel.Panel', {
			border:0,
		    defaults: {
		        //bodyStyle: 'padding:15px'
		    },
		    layout: {
		        type: 'accordion',
		        titleCollapse: true,
		        animate: true,
		        activeOnTop: false
		    }
		});
		AjaxUtils.CallService({
			url: CONTEXT.contextPath+"/v1/navmenu",
			dataType: "json",
			call: function(data){
				if(data.status == "OK"){
					var list = data.entity.menu;;
					for(i in list){
						var menu = list[i];
						var text = menu.menu_name;
						var id = menu.rwid;
						var parentid = menu.parent_menu_id;
						if(parentid =="00000000000000000000000000000001"){
							var filter = "";
							if(!Utils.getCurrentUser().isSuperAdminUser){
								filter = " USER_NO='"+Utils.getCurrentUser().user.user_no+"'";
							}
						 	var tree = Ext.create("Ext.com.bkfm.tree",{
						 		title: text,
						 		layout:'fit',
						 		rootTreeId: id,
						 		rootVisible: true,
						 		rootLabel: text,
						 		treeConfigId: "usermenu",
						 		border: false,
						 		anchor:'100% 100%',
						 		autoScroll: true,
						 		filter: filter,
						 		mappingField: ["menu_url","icon_url"]
						 	});
						 	tree.on("itemclick",function(view,record,item,index,e,opts){
						 		var id = record.get("id");
						 		var title = record.get("text");
						 		var isleaf = record.get("leaf");
						 		if(isleaf){
						 			addTab(id,title);
						 		}
		
						 	})
						 	menuPanel.add(tree);
						}
					}
					var pageBtnMap = data.entity.userMenuBtn;
					Utils.setAllPageBtn(pageBtnMap);
					var modelBtnMap = data.entity.menuModelBtnMap;
					Utils.setAllModelBtn(modelBtnMap);
				}else{
					AjaxUtils.showError('加载系统菜单异常');
				}

			}
		});
		return menuPanel;
	}
};
/**
 * 添加新tab页签
 * id: 菜单id
 * menuUrl: 菜单地址
 */
function addTab(id,title){
	var tab = Ext.getCmp("mainPanel");
	var panel = tab.getComponent('panel_'+id);
	if(!panel){
		panel = Ext.create("Ext.panel.Panel",{
			id: 'panel_'+id,
			frameId: id,
			title: title,
			closable: true,
			html: '<iframe id="frame_'+id+'" name="frame_'+id+'" src="'+CONTEXT.contextPath+'/nav?rwid='+id+'" style="width:100%;height:100%;" frameborder="0" scrolling="no"></iframe>'
		});
		tab.add(panel);
	}
	panel.show();
}